package com.zjhd.sealcutting.controller;

import com.zjhd.sealcutting.service.FileUploadService;
import com.zjhd.sealcutting.utils.OperLog;
import com.zjhd.sealcutting.utils.response.ResponseResult;
import com.zjhd.sealcutting.utils.response.ResultGenerator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * TODO  文件服务
 *
 * @author wangchao
 * @version 1.0
 * @date 2020/11/24 18:08
 */
@Api(value = "文件服务", tags = "文件服务")
@RequestMapping("/file")
@RestController
public class FileUploadController {

    private final static Logger logger = LoggerFactory.getLogger(FileUploadController.class);

    @Autowired
    private FileUploadService fileUploadService;

    @ApiOperation(value = "文件上传", notes = "文件上传接口")
    @PostMapping("/uploadFile")
    @OperLog(operModul = "文件管理-文件上传",operType = "新增",operDesc = "文件上传")
    public ResponseResult uploadFile(HttpServletRequest request) throws Exception{
        try {
            return fileUploadService.uploadFile(request);
        }catch (Exception ex){
            logger.info("文件上传接口处理异常："+ex,this.getClass());
        }
        return ResultGenerator.genFailResult("文件上传失败");
    }

    @ApiOperation(value = "文件下载", notes = "文件下载接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "文件ID",dataType = "String" ,paramType = "path")
    })
    @GetMapping(value = "/download/{id}")
    @OperLog(operModul = "文件管理-文件下载",operType = "查看",operDesc = "文件下载")
    public void download(@PathVariable("id") String id, HttpServletRequest request, HttpServletResponse response) {
        fileUploadService.download(id, request, response);
    }

    @ApiOperation(value = "根据ID查询文件信息", notes = "根据ID查询文件信息接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "文件ID",dataType = "String" ,paramType = "query")
    })
    @GetMapping("/queryByIdFile")
    @OperLog(operModul = "文件管理-作品附件查看",operType = "查看",operDesc = "作品附件查看")
    public ResponseResult queryByIdFile(String id) throws Exception{
        try {
            return fileUploadService.queryByIdFile(id);
        }catch (Exception ex){
            logger.info("根据ID查询文件信息接口处理异常："+ex,this.getClass());
        }
        return ResultGenerator.genFailResult("根据ID查询文件信息失败");
    }

    @ApiOperation(value = "作品图片文件下载", notes = "作品图片文件下载接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "workId",value = "作品ID",dataType = "String" ,paramType = "path")
    })
    @GetMapping(value = "/downloadWorksFile/{workId}")
    @OperLog(operModul = "文件管理-作品图片文件下载",operType = "查看",operDesc = "作品图片文件下载")
    public void downloadWorksFile(@PathVariable("workId") String workId, HttpServletRequest request, HttpServletResponse response)throws Exception {
        fileUploadService.downloadWorksFile(workId, request, response);
    }
}
